package com.example.app05.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.app05.R;
import com.example.app05.entity.Task;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: cx
 * @date: 2022/5/4
 */
public class PushService extends Service {
    private Task task;
    private String channelId = "channelId";
    public class MyBinder extends Binder {
        public void print(){
            Log.i("MyBindService","print");
        }

        public String getData() {
            return "";
        }
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("MyBindService","onBind");
        MyBinder myBinder = new MyBinder();
        List<Task> tasks = new ArrayList<>();
        for(int i=0;i<intent.getIntExtra("size",0);i++){
            task = (Task) intent.getSerializableExtra("task"+i);
            tasks.add(task);
        }
        sendNotification(tasks);
        return myBinder;
    }
    @Override
    public void onCreate() {
        Log.i("MyBindService","onCreate");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.i("MyBindService","onUnbind");
        Log.i("MyBindService","onDestroy");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("MyBindService","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }
    //通知过期的任务
    private void sendNotification(List<Task> taskList){
        //1.创建NotificationManager对象
        NotificationManager manager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        //2.创建NotificationChannel对象（API26后新增）
        NotificationChannel channel = null;
        Notification.Builder builder = null;
        //SDK_INT:程序运行所在系统的API级别
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //当程序运行在API大于等于26的系统时
            channel = new NotificationChannel(channelId,
                    "通知渠道的名称",
                    NotificationManager.IMPORTANCE_DEFAULT);
            //创建通知渠道
            manager.createNotificationChannel(channel);
            //3.创建Notification.Builer对象
            builder = new Notification.Builder(this,
                    channelId);
        }else {//API级别小于26
            //当程序运行在API小于26的系统时
            builder = new Notification.Builder(this);
        }
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = sdf.format(System.currentTimeMillis());
        for(int i=0;i<taskList.size();i++){
            if(taskList.get(i).getDoTime().compareTo(nowTime) < 0){
                //设置通知的属性
                builder.setContentTitle(taskList.get(i).getTopic());
                builder.setContentText("已过期");
                builder.setSmallIcon(R.drawable.notification);
                //Bitmap表示图片的
                builder.setLargeIcon(BitmapFactory.decodeResource(
                        getResources(),R.drawable.baidu));
                //4.创建Notification对象
                Notification notification = builder.build();
                //5.发送通知
                manager.notify(i+1,notification);
            }
        }
    }
}
